package service

// GetStopServer 获取停服信息
func (s *Service) GetStopServer() (bool, error) {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return false, err
	}
	return gmServerConfig.StopServer, nil
}

// SetStopServer 修改停服信息
func (s *Service) SetStopServer(stopServer bool) error {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return err
	}
	gmServerConfig.StopServer = stopServer
	err = s.d.SetGmServerConfig(gmServerConfig)
	if err != nil {
		return err
	}
	return nil
}

// GetIpWhite 获取ip白名单
func (s *Service) GetIpWhite() ([]string, error) {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return nil, err
	}
	return gmServerConfig.IpWhiteList, nil
}

// IpWhiteAdd 添加ip白名单
func (s *Service) IpWhiteAdd(ipWhite string) error {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return err
	}
	for _, ip := range gmServerConfig.IpWhiteList {
		if ip == ipWhite {
			return nil
		}
	}
	gmServerConfig.IpWhiteList = append(gmServerConfig.IpWhiteList, ipWhite)
	err = s.d.SetGmServerConfig(gmServerConfig)
	if err != nil {
		return err
	}
	return nil
}

// IpWhiteDel 删除ip白名单
func (s *Service) IpWhiteDel(ipWhite string) error {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return err
	}
	ipWhiteList := make([]string, 0, len(gmServerConfig.IpWhiteList))
	for _, ip := range gmServerConfig.IpWhiteList {
		if ip == ipWhite {
			continue
		}
		ipWhiteList = append(ipWhiteList, ip)
	}
	gmServerConfig.IpWhiteList = ipWhiteList
	err = s.d.SetGmServerConfig(gmServerConfig)
	if err != nil {
		return err
	}
	return nil
}

// GetUidWhite 获取uid白名单
func (s *Service) GetUidWhite() ([]uint32, error) {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return nil, err
	}
	return gmServerConfig.UidWhiteList, nil
}

// UidWhiteAdd 添加uid白名单
func (s *Service) UidWhiteAdd(uidWhite uint32) error {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return err
	}
	for _, uid := range gmServerConfig.UidWhiteList {
		if uid == uidWhite {
			return nil
		}
	}
	gmServerConfig.UidWhiteList = append(gmServerConfig.UidWhiteList, uidWhite)
	err = s.d.SetGmServerConfig(gmServerConfig)
	if err != nil {
		return err
	}
	return nil
}

// UidWhiteDel 删除uid白名单
func (s *Service) UidWhiteDel(uidWhite uint32) error {
	gmServerConfig, err := s.d.GetGmServerConfig()
	if err != nil {
		return err
	}
	uidWhiteList := make([]uint32, 0, len(gmServerConfig.UidWhiteList))
	for _, uid := range gmServerConfig.UidWhiteList {
		if uid == uidWhite {
			continue
		}
		uidWhiteList = append(uidWhiteList, uid)
	}
	gmServerConfig.UidWhiteList = uidWhiteList
	err = s.d.SetGmServerConfig(gmServerConfig)
	if err != nil {
		return err
	}
	return nil
}
